package net.skyedu.service.system;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.skyedu.bean.OAUser;
import net.skyedu.bean.UserList;
import net.skyedu.dao.OASuper;
import net.skyedu.dao.UserInfoDao;

public class Synch extends HttpServlet {

	private static final long serialVersionUID = -8296275310157004285L;

	/**
	 * Constructor of the object.
	 */
	public Synch() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		int u=Integer.parseInt(request.getParameter("u"));
		String name=request.getParameter("name");
		String currentPage=request.getParameter("currentPage");
		Pattern pattern = Pattern.compile("[0-9]{1,}");
	    Matcher matcher = pattern.matcher((CharSequence)name);
	    boolean result = matcher.matches();
	    if (result == true) {
	    	out.print("<script type='text/javascript'>alert('请先修改姓名,不能为数字');history.go(-1);</script>");
	    }else{
	    	OASuper os=new OASuper();
//	    	System.out.println(name);
	    	OAUser user=os.getEmployee(name);
	    	if(user!=null){
	    		UserInfoDao ud=new UserInfoDao();
	    		UserList ul=new UserList();
	    		ul.setOaId(user.getId());
	    		ul.setPostId(user.getPostId());
	    		ul.setSubdept(user.getDeptName());
	    		ul.setStatus(user.getStatus());
	    		ul.setUserId(u);
	    		ul.setSchoolname(user.getSchoolname());
	    		int num=ud.upOAID(ul);
	    		if(num>0){
	    			out.print("<script type='text/javascript'>alert('同步成功');location.replace('ShowUser?sta=1&currentPage="+currentPage+"');</script>");
	    		}else{
	    			out.print("<script type='text/javascript'>alert('Error:Synch.');history.go(-1);</script>");
	    		}
	    	}else{
	    		out.print("<script type='text/javascript'>alert('员工没有在OA中录入或姓名与OA里的不一致');history.go(-1);</script>");
	    	}
		}
	    }

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {}

}
